﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using Srvtools;

namespace SingleSignOn
{
    public partial class frmSetServerIP : Form
    {
        public frmSetServerIP()
        {
            InitializeComponent();
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            CliUtils.fRemoteIP = tbIP.Text;
            CliUtils.fRemotePort = Convert.ToInt32(tbPort.Text);

            string message = "";
            if (CliUtils.Register(ref message))
            {
                object[] param = new object[2];
                param[0] = Environment.MachineName;
                param[1] = Application.StartupPath;
                object[] myRet = CliUtils.CallMethod("GLModule", "SetSingleSignOnPath", param);
                MessageBox.Show("Register succeed! You need't install it again unless the SingleSignOn's path has changed.");
                if (!File.Exists(param[1] + "\\SingleSignOn.exe.config"))
                {
                    CreateConfig(tbIP.Text, tbPort.Text);
                }
                Environment.Exit(0);
            }
            else
            {
                MessageBox.Show("Register is faild!");
            }

        }

        private void CreateConfig(String IP, String Port)
        {
            FileStream aFileStream = new FileStream(Application.StartupPath + "\\SingleSignOn.exe.config", FileMode.Create, FileAccess.ReadWrite, FileShare.None);
            try
            {
                try
                {
                    XmlTextWriter w = new XmlTextWriter(aFileStream, new System.Text.ASCIIEncoding());
                    w.Formatting = Formatting.Indented;
                    w.WriteStartElement("configuration");
                    w.WriteStartElement("system.runtime.remoting");
                    w.WriteStartElement("application");
                    w.WriteStartElement("client");
                    w.WriteStartElement("wellknown");
                    w.WriteStartAttribute("type");
                    w.WriteValue("Srvtools.LoginService, Srvtools");
                    w.WriteEndAttribute();
                    w.WriteStartAttribute("url");
                    w.WriteValue("http://" + IP + ":" + Port + "/Srvtools.rem");
                    w.WriteEndAttribute();
                    w.WriteEndElement();
                    w.WriteStartElement("wellknown");
                    w.WriteStartAttribute("type");
                    w.WriteValue("Srvtools.ListenerService, Srvtools");
                    w.WriteEndAttribute();
                    w.WriteStartAttribute("url");
                    w.WriteValue("http://" + IP + ":8000/Srvtools.rem");
                    w.WriteEndAttribute();
                    w.WriteEndElement();
                    w.WriteEndElement();
                    w.WriteStartElement("channels");
                    w.WriteStartElement("channel");
                    w.WriteStartAttribute("ref");
                    w.WriteValue("http");
                    w.WriteEndAttribute();
                    w.WriteEndElement();
                    w.WriteEndElement();
                    w.WriteEndElement();
                    w.WriteEndElement();
                    w.WriteEndElement();
                    w.Close();
                }
                finally
                {
                    aFileStream.Close();
                }
            }
            catch (Exception er) { string str = er.Message; }
        }
    }
}